/*
 * @Author: 晴鸢. 3013433857@qq.com
 * @Date: 2025-11-05 08:09:36
 * @LastEditors: 晴鸢. 3013433857@qq.com
 * @LastEditTime: 2025-11-05 19:40:20
 * @FilePath: /Learn/Webself/ServerCal.hpp
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
#pragma once
#include <iostream>
#include "protohol.hpp"
enum{
    Div_Err=1,
    Mod_Err,
    Otherop_Err,
};
class ServerCal
{
public:
    Response CalHelp(const Request &re)
    {
        Response tmp(0, 0);
        switch (re._op)
        {
        case '+':
            tmp._result = re._x + re._y;
            break;
        case '-':
            tmp._result = re._x - re._y;
            break;
        case '*':
            tmp._result = re._x * re._y;
            break;
        case '%':
        {
            if(re._y==0) tmp._dcode=Mod_Err;
            else   tmp._result = re._x % re._y;
            break;
        }
        case '/':
        {
            if(re._y==0) tmp._dcode=Div_Err;
            else tmp._result = re._x / re._y;
            break;
        }
        default:
            tmp._dcode=Otherop_Err;
            break;
        }
        return tmp;
    }

    //提取需求并计算结果
    std::string processor(std::string&package)
    {
        std::string content;
        bool r=Decode(package,content);
        if(!r) return "";
        Request req;
        r=req.Deserialize(content);
        if(!r) return "";
        Response res=CalHelp(req);
        content="";
        res.Serialize(content);
        content=Encode(content);
        return content;
    }

    ~ServerCal(){}
};